package com.ccys.cms.controller.corporateAndInsurance;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitDeeplinkService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitIdentifierService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitSService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.PlanBenefitConsultationDto;
import com.ccys.db.dto.corporateAndInsurance.PlanBenefitDto;
import com.ccys.db.dto.corporateAndInsurance.PlanBenefitPageDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblPlanBenefit;
import com.ccys.db.entity.TblPlanBenefitDeeplink;
import com.ccys.db.entity.TblPlanBenefitIdentifier;
import com.ccys.db.entity.TblPlanBenefitService;
import com.ccys.db.mapper.TblPlanBenefitMapper;
import com.ccys.db.vo.corporateAndInsurance.PlanBenefitConsultationVo;
import com.ccys.db.vo.corporateAndInsurance.PlanBenefitVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/corporateAndInsurance")
@Api(tags = "配套福利管理")
public class PlanBenefitController {

    @Resource
    private PlanBenefitService service;

    @Resource
    private PlanBenefitSService benefitSService;

    @Resource
    private PlanBenefitIdentifierService identifierService;

    @Resource
    private PlanBenefitDeeplinkService deeplinkService;


    @Resource
    private TblPlanBenefitMapper benefitMapper;

    @PostMapping("planBenefit")
    @ApiOperation("新增")
    @Transactional
    public ResponseResult insert(@RequestBody PlanBenefitDto dto) {

       return service.change(dto);
    }

    @PutMapping("planBenefit")
    @ApiOperation("修改")
    @Transactional
    public ResponseResult update(@RequestBody PlanBenefitDto dto) {

        return service.change(dto);
    }


    @GetMapping("planBenefit/detail")
    @ApiOperation("根据id查询")
    public ResponseResult<PlanBenefitDto, Object> getById(String id) {
        TblPlanBenefit entityById = service.findEntityById(id);
        List<TblPlanBenefitService> serviceList = benefitSService.findListByLimit(TblPlanBenefitService.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" benefit_id = '" + id + "'");

        }}, "created_on desc");
        List<TblPlanBenefitIdentifier> identifierList = identifierService.findListByLimit(TblPlanBenefitIdentifier.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" benefit_id = '" + id + "'");

        }}, "created_on desc");
        List<TblPlanBenefitDeeplink> deeplinkList = deeplinkService.findListByLimit(TblPlanBenefitDeeplink.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" benefit_id = '" + id + "'");

        }}, "created_on desc");
        PlanBenefitDto dto = new PlanBenefitDto();
        dto.setPlanBenefit(entityById);
        dto.setServiceList(serviceList);
        dto.setIdentifierList(identifierList);
        dto.setDeeplinkList(deeplinkList);
        return ResponseResult.success(dto);
    }


    @DeleteMapping("planBenefit")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblPlanBenefit model = new TblPlanBenefit();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        //删除benefitService
        benefitSService.deleteBenefitService(id);
        identifierService.deleteIdentifier(id);
        deeplinkService.deleteDeeplink(id);
        return ResponseResult.update(i);
    }


    @GetMapping("planBenefit")
    @ApiOperation(value = "查询分页")
    public ResponseResult<PageInfo<PlanBenefitVo>, Object> getPage(PlanBenefitPageDto dto) {
        return service.getPage(dto);
    }


    @GetMapping("getConsultationList")
    @ApiOperation("查询Consultation list")
    public ResponseResult<List<PlanBenefitConsultationVo>, Object> getConsultationList(PlanBenefitConsultationDto dto) {

        return ResponseResult.success(benefitMapper.getConsultationList(dto));
    }

}
